package com.ti.winona.ui.fragment;

import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.loopj.android.image.SmartImageView;
import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.R;
import com.ti.winona.net.API;
import com.ti.winona.util.FragmentOperateUtil;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MenuLeftFragment extends Fragment {
	
	public final static String TAG = "MenuLeftFragment";
	
	private View mMenuLeftFragmentView;
	
	private SmartImageView mImgSmartLeftUser;
	private TextView mTxtUserName;
	
	private View mLinLeftMenuHomePage;
	private View mLinLeftMenuConversation;
	private View mLinLeftMenuAcademic;
	private View mLinLeftMenuPertool;
	private View mLinLeftMenuPersonalset;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return mMenuLeftFragmentView = inflater.inflate(R.layout.menu_left_fragment, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		findView();
		init();
		initListner();
	}

	private void findView() {
		mImgSmartLeftUser = (SmartImageView) mMenuLeftFragmentView.findViewById(R.id.img_smart_left_user);
		mTxtUserName = (TextView) mMenuLeftFragmentView.findViewById(R.id.txt_user_name);
		
		mLinLeftMenuHomePage = mMenuLeftFragmentView.findViewById(R.id.lin_left_menu_home_page);
		mLinLeftMenuConversation = mMenuLeftFragmentView.findViewById(R.id.lin_left_menu_conversation);
		mLinLeftMenuAcademic = mMenuLeftFragmentView.findViewById(R.id.lin_left_menu_academic);
		mLinLeftMenuPertool = mMenuLeftFragmentView.findViewById(R.id.lin_left_menu_pertool);
		mLinLeftMenuPersonalset = mMenuLeftFragmentView.findViewById(R.id.lin_left_menu_personalset);
	}
	
	private void init() {
		mImgSmartLeftUser.setImageUrl(API.IMAGE + AppConfigFactory.getInstance().getUserInfo().getUserIcon());
		mTxtUserName.setText(AppConfigFactory.getInstance().getUserInfo().getNickname());
	}
	
	private void initListner() {
		// 首页
		mLinLeftMenuHomePage.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				closeOptionsMenu();
				FragmentOperateUtil.popFragmentBackStack(getActivity().getSupportFragmentManager(), "HomeFragment", 0);
			}
		});
		// 我的会话
		mLinLeftMenuConversation.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				closeOptionsMenu();
				FragmentOperateUtil.popFragmentBackStack(getActivity().getSupportFragmentManager(), "HomeFragment", 0);
				addFragment(IMQueueFragment.class, IMQueueFragment.TAG);
			}
		});
		// 学术中心
		mLinLeftMenuAcademic.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				closeOptionsMenu();
				FragmentOperateUtil.popFragmentBackStack(getActivity().getSupportFragmentManager(), "HomeFragment", 0);
				addFragment(AcademicFragment.class, AcademicFragment.TAG);
			}
		});
		// 个人中心
		mLinLeftMenuPertool.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				closeOptionsMenu();
				FragmentOperateUtil.popFragmentBackStack(getActivity().getSupportFragmentManager(), "HomeFragment", 0);
				addFragment(PerToolFragment.class, PerToolFragment.TAG);
			}
		});
		// 个人设置
		mLinLeftMenuPersonalset.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				closeOptionsMenu();
				FragmentOperateUtil.popFragmentBackStack(getActivity().getSupportFragmentManager(), "HomeFragment", 0);
				addFragment(PerSetFragment.class, PerSetFragment.TAG);
			}
		});
	}
	
	private void addFragment(Class<? extends Fragment> clazz, String tag) {
		FragmentOperateUtil.replaceFragment(getActivity().getSupportFragmentManager(), clazz, R.id.main_frame, tag, null, true);
	}
	
	private void closeOptionsMenu() {
		((SlidingFragmentActivity)getActivity()).toggle();
	}
	
}
